/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tech.luoyu.ki.generator;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import tech.luoyu.ki.util.FileUtil;

public class Generator {

	public static void main(String[] args) {
		generate();
	}

	@SuppressWarnings(value = "all")
	private static void generate() {
		String singleFileName = null;// single
		// singleFileName = "SysUserMapper.xml";

		// 1. 删除已经存在的mapper.xml
		File file = new File(Generator.class.getResource("/").getFile());
		File parentFile = file.getParentFile().getParentFile();
		String autoMapperRelativePath = "src\\main\\resources\\mapper\\auto";
		File autoMapperFile = new File(parentFile.getAbsolutePath() + File.separator + autoMapperRelativePath);
		System.out.println(autoMapperFile.getAbsolutePath());
		System.out.println(autoMapperFile.exists());

		if (singleFileName == null) {
			if (autoMapperFile.exists()) {
				FileUtil.delDir(autoMapperFile.getAbsolutePath(), true);
			}

			autoMapperFile.mkdirs();
		} else {
			autoMapperFile = new File(parentFile.getAbsolutePath() + File.separator + autoMapperRelativePath
					+ File.separator + singleFileName);
			if (autoMapperFile.exists()) {
				FileUtil.delFile(autoMapperFile.getAbsolutePath());
			}
		}

		// 2. 重新生成mapper.xml
		try {
			List<String> warnings = new ArrayList<String>();
			boolean overwrite = true;
			ConfigurationParser cp = new ConfigurationParser(warnings);
			InputStream stream = Generator.class.getClassLoader().getResourceAsStream("generatorConfig.xml");
			Configuration config = cp.parseConfiguration(stream);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (IOException ex) {
			Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
		} catch (XMLParserException ex) {
			Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
		} catch (InvalidConfigurationException ex) {
			Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
		} catch (SQLException ex) {
			Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
		} catch (InterruptedException ex) {
			Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
		}

		System.out.println("代码生成完毕");
	}
}
